package com.my12306.train.controller;

import com.my12306.common.model.vo.PageVo;
import com.my12306.common.util.PageUtils;
import com.my12306.common.util.response.ResponseData;
import com.my12306.train.model.entity.RouteEntity;
import com.my12306.train.model.entity.StationEntity;
import com.my12306.train.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 车站路线基本信息api
 *
 * @author wj¦
 * @email
 * @date 2020-07-24 20:58:07
 */
@RestController
@RequestMapping("/route")
public class RouteController {
    @Autowired
    private RouteService routeService;

    /**
     * 列表
     */
    @PostMapping("/pg/lst")
//    @PreAuthorize("")
    public ResponseData list(@RequestBody PageVo pageVo) {
        PageUtils page = routeService.queryRouteByPage(pageVo);
        return ResponseData.ok().put("page", page);
    }
    @GetMapping("/ad/pamts")
    public ResponseData getAddParamters(@RequestParam(value = "routeId",required = false) Long routeId){
        System.out.println("routeId:"+routeId);
        List<StationEntity>stationEntityList =  routeService.getAddParamters();
        ResponseData response = ResponseData.ok().put("stationList", stationEntityList);

        if (routeId!=null) {
         RouteEntity routeEntity= routeService.queryRouteByRouteId(routeId);
         if (routeEntity!=null)
             response= response.put("route",routeEntity);
        }
        return response;
    }

    /**
     * 信息
     */
    @GetMapping("/info/{routeId}")
    public ResponseData info(@PathVariable("routeId") Long routeId) {
        RouteEntity route = routeService.getById(routeId);
        return ResponseData.ok().put("route", route);
    }

    /**
     * 保存
     */
    @PostMapping("/rt/sv")
//    @PreAuthorize("")
    public ResponseData save(@RequestBody RouteEntity route) {
        routeService.saveRoute(route);
        return ResponseData.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/rt/up")
    //@PreAuthorize("")
    public ResponseData update(@RequestBody RouteEntity route) {
        routeService.updateRouteById(route);
        return ResponseData.ok();
    }

    /**
     * 批量删除里程
     */
    @DeleteMapping("/del")
    //    @PreAuthorize("")
    public ResponseData delete(@RequestBody List<Long> routeIds) {
        System.out.println(routeIds);
        routeService.deleteRouteBatchByIds(routeIds);
        return ResponseData.ok();
    }

}
